package com.management.controller;

import com.management.po.User;
import com.management.po.UserAndCompany;
import com.management.service.*;
import com.management.vo.DeptVO;
import com.management.vo.SectionAndPostVO;
import com.management.vo.SectionVO;
import com.management.vo.UserAndCompanyVO;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Timestamp;
import java.util.*;

/**
 * @author zwh
 * 公司员工控制器
 *
 */
@Controller
@RequestMapping("/worker")
public class WorkerController {
    Logger logger=Logger.getLogger(WorkerController.class);
    @Autowired
    UserAndCompanyService userAndCompanyService;
    @Autowired
    DeptService deptService;
    @Autowired
    SectionService sectionService;
    @Autowired
    SectionAndPostService sectionAndPostService;
    @Autowired
    UserService userService;
    //去到添加职员页面
    @RequestMapping("/increase/toaddworker")
    public String toAddWorker(int companyId, Model model){
        logger.info("进入toAddWorker控制器");
        //根据公司编号查询所有部门
        List<DeptVO> listDept = deptService.findByCompanyId(companyId);
        //放入Model
        model.addAttribute("listDept",listDept);
        model.addAttribute("companyId",companyId);
        return "addworker";
    }
    //添加职员
    @RequestMapping("/increase/addworker")
    public String addWorker(String userTel,UserAndCompany userAndCompany){
        logger.info("进入addWorker控制器");
        logger.info(userTel);

        /*UserAndCompany userAndCompany=new UserAndCompany();*/
        userAndCompany.setUserId(userService.findByTel(userTel).getUserId());
        /*userAndCompany.setCompanyId(companyId);
        userAndCompany.setDeptId(deptId);
        userAndCompany.setSectionId(sectionId);
        userAndCompany.setPostId(postId);
        int companyId,int deptId,int sectionId,int postId,double salary
        userAndCompany.setSalary(salary);*/
        //userAndCompany.setCompanyId(1);
        userAndCompany.setEntryTime(new Timestamp(new Date().getTime()));
        userAndCompany.setExpirationDate(new Timestamp(new Date().getTime()));
        userAndCompany.setEmpStateName("在职");
        boolean flag=userAndCompanyService.add(userAndCompany);
        logger.info("添加："+flag);
        return "redirect:http://localhost:8080/management/worker/query/toworkerlist.action?companyId="+userAndCompany.getCompanyId();
    }
    //去到职员列表页面
    @RequestMapping("/query/toworkerlist")
    public String toworkerlist(int companyId,Model model){
        logger.info("进入toworkerlist控制器");
        List<UserAndCompanyVO> list = userAndCompanyService.findByCompanyId(companyId);
        List<Map> worker = new ArrayList<Map>();
        if(list!=null){
            for (UserAndCompanyVO u:list
                 ) {
                int userid = u.getUserId();
                User user = userService.findByUserId(userid);
                Map map = new HashMap();
                map.put("user",user);
                map.put("userAndCompany",u);
                worker.add(map);
            }
        }
        model.addAttribute("worker",worker);
        return "workerlist";
    }
    //去到更新职员信息页面
    @RequestMapping("/editor/toeditworker")
    public String toEditWorker(int userAndCompanyId,Model model){
        logger.info("进入toEditWorker控制器");
        UserAndCompanyVO userAndCompanyVO = userAndCompanyService.findByUserAndCompanyId(userAndCompanyId);
        model.addAttribute("userAndCompanyVO",userAndCompanyVO);
        return "updateworker";
    }


    //更新职员状况
    @RequestMapping("/editor/editworker")
    public String editworker(UserAndCompany userAndCompany){
        logger.info("进入editworker控制器");
        boolean flag=userAndCompanyService.update(userAndCompany);
        logger.info(flag);
        return "redirect:http://localhost:8080/management/worker/query/toworkerlist.action?companyId="+userAndCompany.getCompanyId();
    }
    //去到更新职员信息页面
    @RequestMapping("/editor/toeditworkerdept")
    public String toEditWorkerDept(int userAndCompanyId,int companyId,Model model){
        logger.info("进入toEditWorker控制器");
        //根据公司编号查询所有部门
        List<DeptVO> listDept = deptService.findByCompanyId(companyId);
        //放入Model
        model.addAttribute("listDept",listDept);
        UserAndCompanyVO userAndCompanyVO = userAndCompanyService.findByUserAndCompanyId(userAndCompanyId);
        model.addAttribute("userAndCompanyVO",userAndCompanyVO);
        return "updateworkerdept";
    }

    //sectionajax
    @RequestMapping("/sectionajax")
    @ResponseBody
    public List<Map<String,Object>> sectionAjax(int deptId){
        logger.info("进入ajax循环"+deptId);
        List<Map<String,Object>> list =new ArrayList<>();
        List<SectionVO> sectionVOS = sectionService.findByDeptId(deptId);
        if(sectionVOS!=null){
            for (SectionVO s:sectionVOS
                 ) {
                Map<String,Object> map = new HashMap<>();
                map.put("id",s.getSectionId());
                map.put("name",s.getSectionName());
                list.add(map);
            }
        }
        return list;
    }

    @RequestMapping("/postajax")
    @ResponseBody
    public List<Map<String,Object>> postAjax(int sectionId){
        logger.info("进入ajax循环"+sectionId);
        List<Map<String,Object>> list =new ArrayList<>();
        List<SectionAndPostVO> sectionAndPostVOList = sectionAndPostService.findBySectionId(sectionId);
        if(sectionAndPostVOList!=null){
            for (SectionAndPostVO s:sectionAndPostVOList
                 ) {
                Map<String,Object> map = new HashMap<>();
                map.put("id",s.getPostId());
                map.put("name",s.getPostName());
                list.add(map);
            }
        }
        return list;
    }
}
